<?php
$GLOBALS['lang'] = "am";
require_once('include.php');
define('PATERN_CLASS_FUNCTION','([^a-zA-Z_])');

if( isset($_REQUEST["data"]) ) {
	$DATA = $_REQUEST["data"];
	$data = json_decode($DATA,TRUE);

	if( $data === false ) {	sendError(ERROR_00000);	}
	if( empty($data["className"]) ) { sendError(ERROR_00001); }
	else{ $className = preg_replace(PATERN_CLASS_FUNCTION,'',$data["className"]); }
	if( empty($data["functionName"]) ) { sendError(ERROR_00002); }
	else{ $functionName = preg_replace(PATERN_CLASS_FUNCTION,'',$data["functionName"]); }
	if( empty($data["param"]) ) { $param = array();	}
	else{ $param = $data["param"]; }
	
	$check = new Check();
	$error = new Error();
	$GLOBALS['check'] = &$check;
	$GLOBALS['error'] = &$error;
	$NewClass = new $className();
	if( !empty($check->freeFunctions) ) {
		if( !empty($check->freeFunctions[$functionName]) ) {
			$length = count($param);
			for($i=0; $i<$length; $i++ ) {
				if( !in_array($i+1,$check->freeFunctions[$functionName]) ) {
					if( is_array($param[$i]) ) {
						array_walk_recursive($param[$i], array($check,'superTrim'));
					}
					else{
						$null='null';
						$check->superTrim($param[$i],$null);
					}
				}
			}
		}
	}
	else{
		array_walk_recursive($param, array($check,'superTrim'));
	}


	$json = array();
	$result = call_user_func_array(array(&$NewClass,$functionName), $param);
	if( $result === false ) {
		$json["status"] = false;
		$json["error"] = $error->getUserMessage();
	}
	else{
		$json["status"] = true;
		$json["result"] = $result;
	}
	echo json_encode($json);
}
function sendError($err){
	$json["status"] = false;
	$json["error"] = $err;
	echo json_encode($json);
	exit;
}

?>